﻿using Huirui.Cavan.Application.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.MainBoundedContext.SearchModule;
using Huirui.Cavan.Domain.MainBoundedContext.SystemModule;
using Huirui.Cavan.Domain.Seedwork;

using Huirui.Cavan.Presentation.Seedwork;
using Huirui.Cavan.Presentation.ViewModel.SystemModule;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Huirui.Cavan.Application.MainBoundedContext.StockModule;
using AutoMapper;
using Huirui.Cavan.Core.Extension;
using Newtonsoft.Json;

namespace Huirui.Cavan.Presentation.Controller.SystemModule
{
    public class CustomerController : EntityController<Customer, string, CustomerSearch, ICustomerViewModel, ICustomerAppService>, ICustomerController
    {
        private IAreaAppService AreaAppService { get; set; }
        private IWareHouseAppService WareHouseAppService { get; set; }
        private ICorporationAppService CorporationAppService { get; set; }
        private IRoleAppService RoleAppService { get; set; }

        public override string ControllerName
        {
            get { return ControllerConst.CUSTOMER; }
        }

        public override bool UseDenied
        {
            get { return false; }
        }

        public CustomerController()
        {
            this.Model = ModelFactory.CreateModel<ICustomerViewModel>();
        }

        [ActionName(ActionConst.ADD)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "customer_query")]
        public ActionResult AddCustomer()
        {
            this.Model.CityID = "0201";
            this.Model.Item = new Customer();
            this.Model.Item.WareHouse = new Domain.MainBoundedContext.StockModule.WareHouse();
            this.Model.Item.WareHouse.Id = "001_wh_001";
            return View("~/Views/System/v1/addCustomer.cshtml", this.Model);
        }

        [ActionName(ActionConst.ADD)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "customer_edit")]
        public JsonResult AddCustomer(CustomerViewModel model)
        {
            BoolResult jsonResult = null;
            model.Item.Nature = (SystemIdentifier.CustomerNature)model.Nature;
            model.Item.Area = this.AreaAppService.GetById(model.Item.Area.Id);
            model.Item.WareHouse = this.WareHouseAppService.GetById(model.Item.WareHouse.Id);
            model.Item.AgentType = (SystemIdentifier.AgentType)model.AgentType;
            model.Item.EmployeeRole = this.RoleAppService.GetById(model.EmployeeRole);
            model.Item.PayType = (SystemIdentifier.PayType)model.PayType;
            model.Item.InvoiceType = (SystemIdentifier.InvoiceType)model.InvoiceType;
            if (model.NoPrefixId.IsNotEmpty())
            {
                model.Item.Id = SecurityAppService.Corporation.Id + "_" + model.NoPrefixId;
            }

            BoolGenericResult<Customer> result = this.AppService.AddOrUpdate(model.Item);
            jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "customer_query")]
        public JsonResult GetCustomers(CustomerViewModel model)
        {
            CustomerJsonViewModel json = new CustomerJsonViewModel();
            json.rows = new List<CustomerJson>();

            long total = 0;
            IList<Customer> customers = this.AppService.GetBySearch(SearchFactory.CreateCustomerSearch(model.SearchParam.ID, model.SearchParam.Name, model.SearchParam.Contact, model.SearchParam.AgentTypeID, model.SearchParam.AreaCode), out total).ToList();
            foreach (Customer c in customers)
            {
                CustomerJson cj = Mapper.Map<Customer, CustomerJson>(c);
                json.rows.Add(cj);
            }
            json.total = total;

            return Json(json, "application/json");
        }

        [ActionName(ActionConst.SEARCH)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "customer_query")]
        public ActionResult GetCustomers()
        {
            this.Model.Purpose = RouteData.Values["purpose"].ToString();
            return PartialView("~/Views/System/v1/partial/selectCustomerPartial.cshtml", this.Model);
        }

        [ActionName(ActionConst.LIST)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "customer_query")]
        public ActionResult GetCustomersList()
        {
            return View("~/Views/System/v1/customerList.cshtml", this.Model);
        }

        [ActionName(ActionConst.DETAIL)]
        [HttpGet]
        [ActionPermissionAttribute(Name = "customer_query")]
        public ActionResult GetCustomersList(string customerid)
        {
            CustomerViewModel custvm = new CustomerViewModel();
            Customer  c = this.AppService.GetById(customerid);
            this.Model.Item = c;
            this.Model.AgentType = c.AgentType.GetHashCode() ;
            this.Model.CityID = c.Area.Id;
            this.Model.EmployeeRole = (int)c.EmployeeRole.GetHashCode();
            this.Model.InvoiceType = c.InvoiceType.GetHashCode();
            this.Model.Nature = c.Nature.GetHashCode();
            this.Model.PayType = c.PayType.GetHashCode();
            this.Model.ProvinceID = c.Area.ParentCode;
            return View("~/Views/System/v1/addCustomer.cshtml", this.Model);
        }

        [ActionName(ActionConst.DELETE)]
        [HttpPost]
        [ActionPermissionAttribute(Name = "customer_edit")]
        public JsonResult DeleteCustomer(string selected)
        {
            BoolGenericResult<Customer> result = null;
            List<CustomerJson> customers = JsonConvert.DeserializeObject<List<CustomerJson>>(selected);

            foreach (CustomerJson json in customers)
            {
                Customer c = this.AppService.GetById(json.Id);
                c.Status = false;
                result = this.AppService.Update(c);
            }

            BoolResult jsonResult = new BoolResult(result.IsSuccess, result.Messages);

            return Json(jsonResult, "application/json");
        }
    }
}
